package cn.kone.system.controller;


import cn.kone.base.utlis.R;
import cn.kone.system.annotation.PreAuthorize;
import cn.kone.system.domain.Permission;
import cn.kone.system.query.PermissionQuery;
import cn.kone.system.service.IPermissionService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
@Api(tags = "权限接口类")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;

    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询全部权限")
    @PreAuthorize(name = "查询全部权限", sn = "permission:findAllPermissions",descs = "查询全部权限")
    public R<List<Permission>> findAllPermissions() {

        List<Permission> all = permissionService.findAll();
        return R.ok(all);
    }

    /**
     * 查询单个
     *
     * @param id 用户id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个权限")
    @PreAuthorize(name = "查询单个权限", sn = "permission:get",descs = "查询单个权限")
    public R<Permission> findOnePermissions(@PathVariable("id") Long id) {
        Permission one = permissionService.findOne(id);
        return R.ok(one);
    }

    @PutMapping
    @ApiOperation(value = "新增和修改权限")
    @PreAuthorize(name = "新增和修改权限", sn = "permission:addAndUpdatePermission",descs = "新增和修改权限")
    public R<Void> addAndUpdatePermission(@RequestBody Permission permission) {
        Long id = permission.getId();
        try {
            if (id == null) {
                permissionService.addPermission(permission);
            } else {
                permissionService.updatePermission(permission);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    /**
     * 删除单个
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单个权限")
    @PreAuthorize(name = "删除单个权限", sn = "permission:deletePermissions",descs = "删除单个权限")
    public R<Void> deletePermissions(@PathVariable("id") Long id) {
        try {
            permissionService.deletePermission(id);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    /**
     * 分页查询
     *
     * @param permissionQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询权限")
    @PreAuthorize(name = "分页查询权限", sn = "permission:pageQuery",descs = "分页查询权限")
    public PageInfo<Permission> pageQuery(@RequestBody PermissionQuery permissionQuery) {

        List<Permission> list = permissionService.pageQuery(permissionQuery);
        return new PageInfo<Permission>(list);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除权限")
    @PreAuthorize(name = "批量删除权限", sn = "permission:batchDeletePermission",descs = "批量删除权限")
    public R<Void> batchDeletePermission(@RequestBody Long[] ids) {
        try {
            permissionService.batchDeletePermission(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

}
